/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-14 下午3:44 Created by dwzhang
 */
package com.jade.framework.base.criterion;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Represents an group by operation on entityName.propName.
 *
 * @author dwzhang
 * @version 1.0 13-4-14 下午3:44
 */
public class Group implements Serializable
{
    /** 实体类对象SimpleName的List */
    protected List<String> entityNames = new ArrayList<String>();

    /** 属性名称列表 */
    protected List<String> propNames = new ArrayList<String>();

    /** Constructor */
    public Group()
    {
    }

    /**
     * Constructor
     *
     * @param entityName 实体名称.
     * @param propName   属性名
     */
    public Group(String entityName, String propName)
    {
        entityNames.add(entityName);
        propNames.add(propName);
    }

    /**
     * 新增Group by属性.
     *
     * @param entityName 实体类对象的SimpleName
     * @param propName   属性名称
     */
    public void addGroup(String entityName, String propName)
    {
        entityNames.add(entityName);
        propNames.add(propName);
    }

    public List<String> getEntityNames()
    {
        return entityNames;
    }

    public List<String> getPropNames()
    {
        return propNames;
    }

    /**
     * 获取组装后的名称.
     *
     * @param entityName 实体名称
     * @param propName   属性名称
     * @return 组装后的名称
     */
    public static String getFullName(String entityName, String propName)
    {
        if (entityName == null) {
            return propName;
        }
        else {
            return entityName + "." + propName;
        }
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("GROUP BY ");
        for (int i = 0; i < entityNames.size(); i++) {
            String entityName = entityNames.get(i);
            String propName = propNames.get(i);
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(getFullName(entityName, propName));
        }
        return sb.toString();
    }
}
